package org.one.afternoon.quartz;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.one.afternoon.domain.JobDetails;
import org.one.afternoon.domain.TriggerDetails;
import org.one.afternoon.service.JobService;
import org.one.afternoon.service.TriggerService;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;

public class PersistToDs {


    public static boolean addJob(JobExecutionContext context, JobService jobService, TriggerService triggerService) throws SchedulerException {
        JobDetails jobDetails=new JobDetails();
        jobDetails.setSchedName(context.getScheduler().getSchedulerName());
        jobDetails.setJobName(context.getJobDetail().getKey().getName());
        jobDetails.setJobGroup(context.getJobDetail().getKey().getGroup());
        jobDetails.setJobClassName(String.valueOf(context.getJobDetail().getJobClass()));
        jobDetails.setIsDurable("1");
        jobDetails.setIsNonConCurrent("1");
        jobDetails.setIsUpdateData("1");
        jobDetails.setRequestsRecovery("1");

        LambdaQueryWrapper<JobDetails> jobDetailsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        jobDetailsLambdaQueryWrapper.eq(StringUtils.isNotBlank(jobDetails.getSchedName()),JobDetails::getSchedName,jobDetails.getSchedName());
        boolean jobSave;
        if(jobService.list(jobDetailsLambdaQueryWrapper).size()==0){
            jobSave = jobService.save(jobDetails);
        }else{
            jobSave=true;
        }


        TriggerDetails triggerDetails=new TriggerDetails();
        triggerDetails.setSchedName(context.getScheduler().getSchedulerName());
        triggerDetails.setTriggerName(context.getTrigger().getKey().getName());
        triggerDetails.setTriggerGroup(context.getTrigger().getKey().getGroup());
        triggerDetails.setJobName(context.getJobDetail().getKey().getName());
        triggerDetails.setJobGroup(context.getJobDetail().getKey().getGroup());
        triggerDetails.setPrevFireTime("0");
        triggerDetails.setNextFireTime(String.valueOf(context.getNextFireTime().getTime()));
        triggerDetails.setPriority(5);
        triggerDetails.setTriggerState("BLOCKED");
        triggerDetails.setTriggerType("CRON");
        triggerDetails.setStartTime(context.getTrigger().getStartTime().getTime());
        triggerDetails.setEndTime(0L);
        triggerDetails.setMisfireInstr(context.getRefireCount());

        LambdaQueryWrapper<TriggerDetails> triggerDetailsWrapper = new LambdaQueryWrapper<>();
        triggerDetailsWrapper.eq(StringUtils.isNotBlank(triggerDetails.getSchedName()),TriggerDetails::getSchedName,triggerDetails.getSchedName());
        boolean triggerSave;
        if(triggerService.list(triggerDetailsWrapper).size()==0){
            triggerSave = triggerService.save(triggerDetails);
        }else{
            LambdaUpdateWrapper<TriggerDetails> triggerDetailsLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            triggerDetailsLambdaUpdateWrapper.eq(StringUtils.isNotBlank(triggerDetails.getSchedName()),TriggerDetails::getSchedName,triggerDetails.getSchedName())
                    .set(StringUtils.isNotBlank(triggerDetails.getJobName()),TriggerDetails::getJobName,triggerDetails.getJobName())
                    .set(StringUtils.isNotBlank(triggerDetails.getJobGroup()),TriggerDetails::getJobGroup,triggerDetails.getJobGroup())
                    .set(triggerDetails.getNextFireTime()!=null,TriggerDetails::getNextFireTime,triggerDetails.getNextFireTime())
                    .set(triggerDetails.getPrevFireTime()!=null,TriggerDetails::getPrevFireTime,triggerDetails.getPrevFireTime())
                    .set(triggerDetails.getNextFireTime()!=null,TriggerDetails::getNextFireTime,triggerDetails.getNextFireTime())
                    .set(triggerDetails.getStartTime()!=null,TriggerDetails::getStartTime,triggerDetails.getStartTime())
                    .set(triggerDetails.getEndTime()!=null,TriggerDetails::getEndTime,triggerDetails.getEndTime())
                    .set(triggerDetails.getMisfireInstr()!=null,TriggerDetails::getMisfireInstr,triggerDetails.getMisfireInstr());
            triggerSave = triggerService.update(triggerDetailsLambdaUpdateWrapper);
        }

        return jobSave && triggerSave;
    }
}
